package com.ruoyi.web.controller.datastatistics;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.datastatistics.domain.PublicShareComsumeReportBo;
import com.ruoyi.datastatistics.domain.PublicShareReport;
import com.ruoyi.datastatistics.domain.PublicShareReportBo;
import com.ruoyi.datastatistics.service.IPublicShareComsumeReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.datastatistics.domain.PublicShareComsumeReport;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公摊报表Controller
 *
 * @author zt
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/datastatistics/publicShareComsumeReport")
public class PublicShareComsumeReportController extends BaseController
{
    @Autowired
    private IPublicShareComsumeReportService publicShareComsumeReportService;

    /**
     * 查询公摊报表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PublicShareComsumeReport PublicShareComsumeReport)
    {
        startPage();
        PublicShareComsumeReport.setTenantId(getLoginUser().getTenantId());
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.selectPublicShareComsumeReportList(PublicShareComsumeReport);
        return getDataTable(list);
    }

    /**
     * 导出公摊报表列表
     */
    @Log(title = "公摊报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PublicShareComsumeReport PublicShareComsumeReport)
    {
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.selectPublicShareComsumeReportList(PublicShareComsumeReport);
        ExcelUtil<PublicShareComsumeReport> util = new ExcelUtil<PublicShareComsumeReport>(PublicShareComsumeReport.class);
        util.exportExcel(response, list, "公摊报表数据");
    }


    /**
     * 导出公摊报表列表
     */
    @Log(title = "公摊报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportForBill")
    public void exportForBill(HttpServletResponse response, PublicShareComsumeReport PublicShareComsumeReport)
    {
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.selectPublicShareComsumeReportList(
                PublicShareComsumeReport);

        List<PublicShareComsumeReportBo>  publicShareComsumeReportBoList= list.stream()
                .map(e -> {
                    PublicShareComsumeReportBo e2 = new PublicShareComsumeReportBo();
                    BeanUtils.copyProperties(e, e2);
                    return e2;
                }) .collect(Collectors.toList());

        ExcelUtil<PublicShareComsumeReportBo> util = new ExcelUtil<PublicShareComsumeReportBo>
                (PublicShareComsumeReportBo.class);
        util.exportExcel(response, publicShareComsumeReportBoList, "公摊报表数据");
    }




    /**
     * 获取公摊报表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(publicShareComsumeReportService.selectPublicShareComsumeReportById(id));
    }

    /**
     * 新增公摊报表
     */
    @Log(title = "公摊报表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PublicShareComsumeReport PublicShareComsumeReport)
    {
        return toAjax(publicShareComsumeReportService.insertPublicShareComsumeReport(PublicShareComsumeReport));
    }

    /**
     * 修改公摊报表
     */
    @Log(title = "公摊报表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PublicShareComsumeReport PublicShareComsumeReport)
    {
        return toAjax(publicShareComsumeReportService.updatePublicShareComsumeReport(PublicShareComsumeReport));
    }

    /**
     * 删除公摊报表
     */
    @Log(title = "公摊报表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(publicShareComsumeReportService.deletePublicShareComsumeReportByIds(ids));
    }

    /**
     * @Description: 公摊报表-独立
     * @date: 2025/5/15 16:56
     * @author: zt
     */
    @GetMapping("/selectConsumeReportList")
    public TableDataInfo selectConsumeReportList(PublicShareComsumeReport publicShareComsumeReport)
    {
        Map summaryMap = publicShareComsumeReportService.selectComsumeDetailListSum(publicShareComsumeReport);
        startPage();
        SysUser user = getLoginUser().getUser();
        if(user.getSystemFlag() != Constants.SYS_FLAG_ADMIN){
            publicShareComsumeReport.setTenantId(user.getTenantId());
        }
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.selectComsumeDetailList(publicShareComsumeReport);
        return getDataTable(list, summaryMap);
    }



    @PostMapping("/exportComsumeReportList")
    public void exportProductReportList(HttpServletResponse response, PublicShareComsumeReport publicShareComsumeReport) throws ParseException {
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.selectComsumeDetailList(publicShareComsumeReport);

        ExcelUtil<PublicShareComsumeReport> util = new ExcelUtil<>
                (PublicShareComsumeReport.class);
        util.exportExcel(response, list , "用户公摊明细");
    }


    /**
     * 查询公摊报表列表
     */
    @GetMapping("/publicShareListForBill")
    public TableDataInfo publicShareListForBill(PublicShareComsumeReport PublicShareComsumeReport)
    {
        startPage();
        PublicShareComsumeReport.setTenantId(getLoginUser().getTenantId());
        List<PublicShareComsumeReport> list = publicShareComsumeReportService.publicShareListForBill(PublicShareComsumeReport);
        return getDataTable(list);
    }
}
